using huqiang.Core.HGUI;
using System;
using UnityEngine;
using huqiang.UIComposite;
using huqiang.UIEvent;
using huqiang;
using huqiang.Data;
using huqiang.UIModel;

public class TreeViewPage : UIPage
{
    //反射UI界面上的物体
    class View
    {
        public TreeView treeView;
    }
    View view;
    public override void Initial(UIElement parent, object dat = null)
    {
        base.Initial(parent, dat);
        view = LoadUI<View>("baseUI", "TreeViewPage");//"baseUI"创建的bytes文件名,"page"为创建的页面名
        InitialData();
        InitialEvent();
    }
    /// <summary>
    /// 扩展使用见 TreeViewPageA
    /// </summary>
    void InitialData()
    {
        TreeViewNode root = new TreeViewNode();
        root.content = "root";
        TreeViewNode node11 = new TreeViewNode();
        node11.content = "node11";
        for(int i=0;i<11;i++)
        {
            TreeViewNode node = new TreeViewNode();
            node.content = "node11"+i;
            node11.Add(node);
        }
       
        TreeViewNode node12 = new TreeViewNode();
        node12.content = "node12";
        for (int i = 0; i < 20; i++)
        {
            TreeViewNode node = new TreeViewNode();
            node.content = "node12" + i;
            node12.Add(node);
        }
        root.Add(node11);
        root.Add(node12);
        view.treeView.Root = root;
        
    }
    void InitialEvent()
    {
        view.treeView.SetItemUpdate<TreeViewItem, TreeViewNode>(ItemUpdate);
        view.treeView.Refresh();
    }
    void ItemUpdate(TreeViewItem item,TreeViewNode node)
    {
        item.Text.Text = node.content;
        item.Item.DataContext = item;
        item.Item.Click = (o, e) => {
            var i = o.DataContext as TreeViewItem;
            if(i.node.child.Count>0)
            {
                i.node.expand = !i.node.expand;
                view.treeView.Refresh();
            }
        };
    }
}
